package dao;


import pojo.Users;
import pojo.ViolationRecords;

import java.util.List;

public class UsersDao extends BaseDao {

    /**
     * 向数据库添加一条user数据
     *
     * @param user
     * @return增加成功返回1，失败返回0
     */
    public int addUser(Users user) {
        String sql = "insert into users(user_id,user_name,password,is_admin,status) values(DEFAULT,?,?,DEFAULT,DEFAULT)";
        int row = baseUpdate(sql, user.getUserName(), user.getPassword());
        System.out.println(row);
        return row;
    }

    public Users findByUsername(String username) {
        String sql = "SELECT user_id AS userId,user_name AS userName,password,is_admin AS isAdmin,status FROM users WHERE user_name =?";
        List<Users> usersList = baseQuery(Users.class, sql, username);
//        迭代遍历
        usersList.forEach(System.out::println);
        return usersList != null && usersList.size() > 0 ? usersList.get(0) : null;
    }

    public int findAdminByUserId(Integer userId) {
        String sql = "select is_admin from users where user_id=?";
        Integer isAdmin = baseQueryObject(Integer.class,sql,userId);
        return isAdmin;
    }

    public List<Users> findAllUser() {
        System.out.println("这里是findAllUser的Dao层");
        String sql = "select user_id userId,user_name userName,is_admin isAdmin,status,violation_count violationCount from users";
        List<Users> list = baseQuery(Users.class,sql);
//        迭代遍历
        list.forEach(System.out::println);
        return list;
    }

    public int upUser(Integer userId) {
        System.out.println("这里是borrowBook的Dao层");
        String sql = "update users set is_admin = 1 where user_id=?";
        int row = baseUpdate(sql,userId);
        return row;
    }

    public int checkStatus(Integer userId) {
        String sql = "select status from users where user_id=?";
        Integer status = baseQueryObject(Integer.class,sql,userId);
        return status;
    }

    public List<ViolationRecords> findAllViolation() {
        System.out.println("这里是findAllViolation的Dao层");
        String sql = "select violation_id violationId,user_id userId,book_id bookId,overdue_days overdueDays,record_date recordDate from violation_records";
        List<ViolationRecords> list = baseQuery(ViolationRecords.class,sql);
//        迭代遍历
        list.forEach(System.out::println);
        return list;
    }

    public int unlockUser(int userId) {
        System.out.println("这里是unlockUser的Dao层");
        String sql = "update users set status = 1 where user_id=?";
        int row = baseUpdate(sql,userId);
        return row;
    }

    public int deleteUser(int userId) {
        System.out.println("这里是unlockUser的Dao层");
        String sql = "delete from users where user_id=?";
        int row = baseUpdate(sql,userId);
        return row;
    }
}

//    public Users findByUseridPassword(Integer userId, String password) throws SQLException, ClassNotFoundException {
//        Connection conn = null;
//        Statement stmt = null;
//        ResultSet rs = null;
////        获得数据的连接
//        conn = JDBCUtil.getConnection();
//        try {
////        获得Statement对象
//            stmt = conn.createStatement();
////        发送SQL语句
//            String sql = "select user_id userId, user_name userName, password from news_user where user_name = " + userId + " and password = "+password;
//            rs = stmt.executeQuery(sql);
////        处理结果集
//            while(rs.next()){
//                Users user = new Users();
//                user.setUserId(rs.getInt("userId"));
//                user.setUserName(rs.getString("userName"));
//                user.setPassword(rs.getString("password"));
//                user.setIsAdmin(rs.getInt("isAdmin"));
//                user.setStatus(rs.getInt("status"));
//            }
//            return null;
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } finally {
//            JDBCUtil.release(rs, stmt,conn);
//        }
//    }


//
//    public Users findByUsername(String username) {
//        String sql = "select uid, username, user_pwd userPwd, nick_name nickName from news_user where username = ? ";
//        List<Users> newsUserList = baseQuery(Users.class, sql, username);
//        return newsUserList != null && newsUserList.size() > 0 ? newsUserList.get(0):null;
//    }
//
//
//    public Users findById(Integer userId) {
//        String sql = "select uid, username, user_pwd userPwd, nick_name nickName from news_user where uid = ? ";
//        List<Users> newsUserList = baseQuery(Users.class, sql, userId);
//        return newsUserList != null && newsUserList.size() > 0 ? newsUserList.get(0):null;
//    }
//
//
//    public Integer insertUser(Users registUser) {
//        String sql = "insert into news_user values (DEFAULT,?,?,?)";
//        return baseUpdate(sql,
//                registUser.getUserId(),
//                registUser.getPassword(),
//                registUser.getUserName()
//        );
//    }

